<?php 
/**
 * IMMOPHP
 * Gestion des images 
 * @author Drouche Romain
 * */
require 'kernel/common.php';

if(!$annonce_id = retrieve('GOST', 'id', true)) redirect('annonce.php?error=no_annonce_select');

$Annonce_adm = new Annonce_adm($paramClass);

# Changement de la photo par defaut
if( isset($_GET['make_photo'])) 
    $Annonce_adm->setMakeDefautPhoto( retrieve('GET', 'id'), retrieve('GET', 'photo_id') );     // Marque la photo par defaut

# Ajout nouvelle photo
if( isset($_POST['send_photo']) ) 
{   
    if( $Annonce_adm->setUploadPhoto($annonce_id) )                                             // Upload la photo sur le serveur
        $Annonce_adm->printErrorMessage($lang['Photo_ajoutee']);                                // Affichage message
    else
        $Annonce_adm->printErrorMessage($lang['Erreur_ajout_photo']);                           // Affichage erreur    
}

if(! $Annonce_adm->getAnnonceInfo($annonce_id) ) redirect('annonce.php?error=annonce_not_exist');   // Recuperation information annonce	

# Suppression photo
if( isset($_GET['delete']) )
{
    if( $Annonce_adm->setDeletePhoto2(retrieve('GET', 'photo_id')) )
        $Annonce_adm->printErrorMessage( $lang['Photo_supprimee'] );
    else
        $Annonce_adm->printErrorMessage( 'Error delete photo' );
}

# Construction du tableau des images
$t_image = '<tr>';
$i = 1;
	
$Sql = $db->sql_query("SELECT * FROM immo_annonce_photo WHERE annonce = '". $annonce_id ."'");
while($Photo = $db->sql_fetchobject($Sql))
{
    $t_image .= '<td class="td_border_w" align="center"><a href="' . $config['url'].'upload/annonces/'. $annonce_id . '/' . $Photo->photo . '" rel="lightbox" title=""><img src="'.$config['url'].'upload/annonces/' . $annonce_id . '/' . $Photo->photo . '" alt="" style="border:none; height:150px; width:200px;" /></a>';

    // Test admin ou posteur
    if($Session->_session['administrateur'] == 'yes' || $Session->_session['iduser'] == $Annonce_adm->annonce['user'])
    {
        if( $Annonce_adm->annonce['photo_principale'] != $Photo->photo )
            $t_image .= '<br /><hr />&nbsp;&nbsp;<a href="javascript:delphoto(\'' . $Photo->id . '\');"><img src="images/del.png" style="border:none; width:20px; height:20px;" /></a>&nbsp;&nbsp;&nbsp;<a href="annonce_image_ajout.php?id='. $annonce_id .'&amp;photo_id='. $Photo->id .'&amp;make_photo&token='.$Session->_session['token'].'" title=""><img src="images/defaut_photo_make.png" style="border:none; height: 20px; width:20px:" alt="Make a dafault photo"/></a>';    
	}
       
	$t_image .='</td>';
    
	$i++;
	
	if($i == 4)
	{
		$t_image .='</tr><tr>';
		$i=1;
	}
}
$t_image .= '</tr>';    // Fermeture ligne

# Envoie a smarty pour affichage
$smarty->assign('Annonce', $Annonce_adm->annonce);
$smarty->assign('lang', $lang);
$smarty->assign('tab_photo', $t_image);
$smarty->assign('token', $Session->_session['token']);
$smarty->display('annonce_image_ajout.tpl');

require 'footer.php';

?>